package com.keyinong.restaurant.tools;

import java.io.UnsupportedEncodingException;

import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.entity.StringEntity;
import org.json.JSONObject;

import android.content.Context;

import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestParams;

public class HttpTools {
	Context context;
	AsyncHttpClient client=new AsyncHttpClient();
	
	public HttpTools(Context context) {
		super();
		this.context = context;
	}
	/**登录*/
	public void login(String phone, String password,
			JsonHttpResponseHandler backcall) {
		RequestParams params = new RequestParams();
		params.put("phone", phone);
		params.put("password", password);
		client.post(context, Url.loginUrl, params, backcall);
	}
	/**确认*/
	public void queren(String token, String orderid,
			JsonHttpResponseHandler backcall) {
		RequestParams params = new RequestParams();
		params.put("token", token);
		params.put("orderid", orderid);
		client.post(context, Url.querenUrl, params, backcall);
	}
	/**已完成*/
	public void yiwancheng(String token, String orderid,String state,String paymode,
			JsonHttpResponseHandler backcall) {
		RequestParams params = new RequestParams();
		params.put("token", token);
		params.put("orderid", orderid);
		params.put("state", state);
		params.put("paymode", paymode);
		client.post(context, Url.querenUrl, params, backcall);
	}
	/**确认取货*/
	public void finishOrder(String token, String orderid,
			JsonHttpResponseHandler backcall) {
		RequestParams params = new RequestParams();
		params.put("token", token);
		params.put("orderid", orderid);
		client.post(context, Url.finishOrder, params, backcall);
	}
	
	/**订单列表*/
	public void order(String token, String page,String offset,String type,String recmode,
			JsonHttpResponseHandler backcall) {
		RequestParams params = new RequestParams();
		params.put("token", token);
		params.put("page", page);
		params.put("offset", offset);
		params.put("type", type);
		params.put("recmode", recmode);
		client.post(context, Url.orderUrl, params, backcall);
	}
	/**直销订单列表*/
	public void getSaleOrders(String token, String page,String offset,
			JsonHttpResponseHandler backcall) {
		RequestParams params = new RequestParams();
		params.put("token", token);
		params.put("page", page);
		params.put("offset", offset);
		client.post(context, Url.getSaleOrders, params, backcall);
	}
	/**订单详情*/
	public void detail(String token, String orderid,
			JsonHttpResponseHandler backcall) {
		RequestParams params = new RequestParams();
		params.put("token", token);
		params.put("orderid", orderid);
		client.post(context, Url.detailUrl, params, backcall);
	}
	/**打印订单*/
	public void printorder(String token, String orderid,
			JsonHttpResponseHandler backcall) {
		RequestParams params = new RequestParams();
		params.put("token", token);
		params.put("orderid", orderid);
		client.post(context, Url.printorderUrl, params, backcall);
	}
	/**取消订单*/
	public void cancleorder(String token, String orderid,
			JsonHttpResponseHandler backcall) {
		RequestParams params = new RequestParams();
		params.put("token", token);
		params.put("orderid", orderid);
		client.post(context, Url.cancleorderUrl, params, backcall);
	}
	/**获得用户信息*/
	public void getUserInfo(String token, String no,
			JsonHttpResponseHandler backcall){
		RequestParams params = new RequestParams();
		params.put("token", token);
		params.put("no", no);
		client.post(context, Url.getUserInfo, params, backcall);
	}
	
	/**充值*/
	public void recharge(String token, String userid,String num,String type,
			JsonHttpResponseHandler backcall){
		RequestParams params = new RequestParams();
		params.put("token", token);
		params.put("userid", userid);
		params.put("num", num);
		params.put("type", type);
		client.post(context, Url.recharge, params, backcall);
	}
	
	/**保存订单结算功能*/
	public void saveNewOrder(String token, String total,
			JsonHttpResponseHandler backcall){
		RequestParams params = new RequestParams();
		params.put("token", token);
		params.put("total", total);
//		JSONObject jsonObject = new JSONObject();
//		StringEntity entity=null;
//		try {
//		jsonObject.put("token",token);
//        jsonObject.put("total",total);
//         entity=new StringEntity(jsonObject.toString());
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
//		 ByteArrayEntity entity = null;
//		 try {
//             entity = new ByteArrayEntity(jsonObject.toString().getBytes("UTF-8"));
////             entity.setContentType(new );
////             entity.setContentType(new H);
//         } catch (UnsupportedEncodingException e) {
//             e.printStackTrace();
//         }
		
		client.post(context, Url.saveNewOrder, null, 
				params, null, backcall);
//		client.post(context, Url.saveNewOrder, entity,
//				"application/json", backcall);
//		client.post(context,Url.saveNewOrder, params,"application/json",backcall);
	}
	
	/**支付处理*/
	public void otherPay(String token, String orderid,String no,
			JsonHttpResponseHandler backcall){
		RequestParams params = new RequestParams();
		params.put("token", token);
		params.put("orderid", orderid);
		params.put("no", no);
		client.post(context, Url.otherPay, params, backcall);
	}
	
	
}
